﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DIPLib
{
    public partial class GammaSeekBar : UserControl
    {
        Graphics mGraphics;
        SolidBrush mBrush;
        private Bitmap mArrow;
        int xLeft = 10, yTop = 34, xCurrent; // xCurrent la vi tri x cua mui ten cua con tro, tinh tu ben trai qua
        public event EventHandler ValuesChanged;
        float mGamma = 1f;
        bool isSeeking;

        public float Gamma
        {
            get
            {
                return mGamma;
            }
            set
            {
                mGamma = value;
            }
        }

        public GammaSeekBar()
        {
            InitializeComponent();
            mArrow = DIPLib.Properties.Resources.arrow;
            mArrow.MakeTransparent(Color.FromArgb(255, 255, 255));
            mBrush = new SolidBrush(Color.Red);
            xCurrent = xLeft + 160;
        }

        private void GammaSeekBar_Paint(object sender, PaintEventArgs e)
        {
            mGraphics = e.Graphics;
            mGraphics.FillRectangle(mBrush, xLeft, yTop, 250, 3);

            mGraphics.DrawImage(mArrow, xCurrent - 4, yTop + 4, 9, 14);
            txtGamma.Text = String.Format("{0:F2}", mGamma);
        }

        private void GammaSeekBar_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y >= yTop + 4 && e.Y <= yTop + 18)
            {
                if (e.X >= 6 && e.X <= 264)
                {
                    isSeeking = true;
                }
            }
        }

        private void GammaSeekBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (isSeeking)
            {
                xCurrent = e.X + 4;
                if (xCurrent >= 10 && xCurrent < 260)
                {
                    if (xCurrent <= 170)
                    {
                        mGamma = (float)Math.Round((xCurrent - 10) * 0.00625f, 2);
                    }
                    else
                    {
                        mGamma = mGamma = (float)Math.Round(1 + (xCurrent - 170) * 0.1f, 2); ;
                    }
                    Invalidate();
                }
            }
        }

        private void GammaSeekBar_MouseUp(object sender, MouseEventArgs e)
        {
            if (isSeeking)
            {
                xCurrent = e.X + 4;
                if (xCurrent >= 10 && xCurrent < 260)
                {
                    if (xCurrent <= 170)
                    {
                        mGamma = (float)Math.Round((xCurrent - 10) * 0.00625f, 2);
                    }
                    else
                    {
                        mGamma = mGamma = (float)Math.Round(1 + (xCurrent - 170) * 0.1f, 2); ;
                    }
                    Invalidate();
                }
                isSeeking = false;
                if (ValuesChanged != null)
                    ValuesChanged(this, new EventArgs());
            }
        }

        private void txtGamma_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                try
                {
                    mGamma = float.Parse(txtGamma.Text);
                    if (mGamma < 0 || mGamma > 10)
                        return;
                    if (mGamma > 1)
                    {
                        xCurrent = (int)((mGamma - 1) / 0.1f) + 170;
                    }
                    else
                    {
                        xCurrent = (int)(mGamma / 0.00625f) + 10;
                    }
                    Invalidate();
                    if (ValuesChanged != null)
                        ValuesChanged(this, new EventArgs());
                }
                catch (System.Exception ex)
                {
                    Console.Error.WriteLine(ex.Message);
                }
            }
        }

    }
}
